package com.aruba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.aruba.handler.MyBlockHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Demo3Controller {

    @RequestMapping("/demo8")
    @SentinelResource(value = "demo8", blockHandler = "blockHandler")
    public String demo8(Integer id) {
        return "demo8";
    }

    public String blockHandler(Integer id, BlockException e) {
        return "您被限制访问" + id;
    }

    @RequestMapping("/demo9")
    @SentinelResource(value = "demo9",
            blockHandler = "myBlockHandler",
            blockHandlerClass = MyBlockHandler.class)
    public String demo9(Integer id) {
        return "demo9";
    }

    @RequestMapping("/demo10")
    @SentinelResource(value = "demo10",
            fallback = "fallback")
    public String demo10(Integer id) {
        if (id == null) throw new NullPointerException("id 不能为空");
        return "demo10";
    }

    public String fallback(Integer id, Throwable e) {
        return "服务器异常 " + e.getMessage();
    }
}
